package keeto.com.vn.classified.task;

import java.io.IOException;

import keeto.com.vn.classified.core.ClassifiedCore;
import keeto.com.vn.classified.core.ClassifiedListener;
import keeto.com.vn.classified.network.IProtocol;
import keeto.com.vn.classified.request.RegisterRequest;
import keeto.com.vn.classified.response.RegisterResponse;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

public class TaskRegister extends AbstractTask{
	String username,password, phone;
	public TaskRegister(ClassifiedListener listener, ClassifiedCore core,String username,String pass, String phone) {
		super(listener, core);
		// TODO Auto-generated constructor stub
		this.username=username;
		this.password=pass;
		this.phone=phone;
	}

	@Override
	public void run() throws ClientProtocolException, JSONException,
			IOException {
		// TODO Auto-generated method stub
		RegisterRequest request = new RegisterRequest();
		request.setUsername(username);
		request.setPassword(password);
		request.setPhone(phone);
		RegisterResponse response;
		response = json.register(request);
		if (core.filterResponseReturnCode(response.getReturnCode())) {
			return ;
		}
		for (ClassifiedListener listener : listeners) {
			listener.finishRegister(response.getReturnCode(),username,password);
		}
		
	}

	@Override
	public void timeoutCallback() {
		// TODO Auto-generated method stub
		for (ClassifiedListener listener : core.copyAndReconstructListeners(null)) {
			listener.finishRegister(IProtocol.STATUS_CODE_CONNECTION_TIMEOUT_502,
					username,password);
		}
	}

}
